package com.project.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dtyunxi.rest.RestCode;
import com.dtyunxi.rest.RestResponse;
import com.project.common.Constants;
import com.project.entity.user.Member;
import com.project.entity.user.User;
import com.project.service.IAreaService;
import com.project.service.IMemberService;
import com.project.service.IUserService;
import com.project.util.EhcacheUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("api/member")
@Api(value = "api/member", description = "会员管理")
public class MemberController {
	
	private static final Logger logger = LoggerFactory.getLogger(MemberController.class);
	
	@Autowired
	private IUserService userService;
	@Autowired
	private IMemberService memberService;
	@Autowired
	private IAreaService areaService;
	
	@ApiOperation(value = "保存会员信息", notes = "保存会员信息",response = Long.class)
    @RequestMapping(value = "save", method = RequestMethod.POST)
	public RestResponse saveMember(HttpServletRequest request,
			@ApiParam(required = true, name = "member", value = "会员信息")@ModelAttribute Member member){
		User user = (User)request.getSession().getAttribute(Constants.CURRENT_LOGIN_USER);
		Member memberDto = memberService.getByUserId(user.getId());
		if(memberDto!=null)
			return new RestResponse(RestCode.RESULT_SYSTEM_ERROR,"不能重复申请入会");
		member.setUserCode(user.getUserCode());
		member.setMobile(user.getMobile());
		member.setApplicationTime(new Date());
		member.setUserId(user.getId());
		member.setAuditStatus("W");//待审核
		memberService.save(member);
		//会员信息跟新到用户信息
		if(StringUtils.isNotBlank(member.getName()))
			user.setName(member.getName());
		if(StringUtils.isNotBlank(member.getEmail()))
			user.setEmail(member.getEmail());
		if(StringUtils.isNotBlank(member.getProvince()))
			user.setProvince(member.getProvince());
		if(StringUtils.isNotBlank(member.getCity()))
			user.setCity(member.getCity());
		if(StringUtils.isNotBlank(member.getDistrict()))
			user.setDistrict(member.getDistrict());
		if(StringUtils.isNotBlank(member.getAddress()))
			user.setAddress(member.getAddress());
		if(StringUtils.isNotBlank(member.getCompany()))
			user.setCompany(member.getCompany());
		if(StringUtils.isNotBlank(member.getPosition()))
			user.setPosition(member.getPosition());
		if(StringUtils.isNotBlank(member.getAdeptField()))
			user.setAdeptField(member.getAdeptField());
		if(StringUtils.isNotBlank(member.getIntro()))
			user.setIntro(member.getIntro());
		if(StringUtils.isNotBlank(member.getHonor()))
			user.setHonor(member.getHonor());
		if(StringUtils.isNotBlank(member.getWritings()))
			user.setWritings(member.getWritings());
		userService.update(user);
		String auth = request.getHeader("auth");
		logger.info("------------------saveMember保存会员信息.更新缓存auth:{}", auth);
		EhcacheUtil.put(Constants.LOGIN_USER_CACHE, auth, user);
		request.getSession().setAttribute(Constants.CURRENT_LOGIN_USER, user);
		return new RestResponse(member.getId());
	}
	
	@ApiOperation(value = "获取会员信息", notes = "获取会员信息",response = Member.class)
    @RequestMapping(value = "ops", method = RequestMethod.GET)
	public RestResponse getMember(HttpServletRequest request){
		User user = (User)request.getSession().getAttribute(Constants.CURRENT_LOGIN_USER);
		Member member = null;
		if(user!=null){
			member = memberService.getByUserId(user.getId());
			if(member!=null){
				if (StringUtils.isNotEmpty(member.getProvince()))
					member.setProvinceName(areaService.getAreaNameByCode(member.getProvince()));
				if (StringUtils.isNotEmpty(member.getCity()))
					member.setCityName(areaService.getAreaNameByCode(member.getCity()));
				if (StringUtils.isNotEmpty(member.getDistrict()))
					member.setDistrictName(areaService.getAreaNameByCode(member.getDistrict()));
			}
		}
		return new RestResponse(member);
	}
	
	
}
